package com.judian.smart.factory;

import com.judian.smart.R;
import com.judian.smart.base.MyApplication;
import com.judian.smart.model.Room;
import com.judian.smart.proxy.WidgetDAOProxy;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Peter
 * date: 2019/08/09 星期五 10:50
 * desc:
 */
public class RoomFactory {
    private volatile static RoomFactory install;

    public static RoomFactory getInstance() {
        if (null == install) {
            install = new RoomFactory();
        }
        if (list.size() == 0) {
            WidgetDAOProxy widgetDAOProxy = WidgetDAOProxy.getInstance();
            list = widgetDAOProxy.getRoom();
        }
        return install;
    }

    private static List<Room> list = new ArrayList<>();


    public static List<Room> getAll() {
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }
        return list;
    }

    public Room getRoomByName(String roomName) {
        Room room = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }
        for (Room _room : list) {
            if (_room.getRoomName().equals(roomName)) {
                room = _room;
                break;
            }
        }
        return room;
    }

    /**
     * 获取房间列表
     *
     * @param flag -1 所有  0 隐藏  1 显示  2 中控
     * @return 传入的房间类型列表
     */
    public List<Room> getRoom(int flag) {
        List<Room> resultList = new ArrayList<>();
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }
        for (Room room : list) {
            if (flag == -1) {
                resultList.add(room);
            } else {
                if (room.getIsShow() == flag) {
                    resultList.add(room);
                }
            }
        }
        return resultList;
    }

    public int getRoomControl() {
        int result = 0;
        for (Room room : list) {
            if (room.getIsShow() == 2) {
                result = 1;
                break;
            }
        }
        return result;
    }

    public List<String> getRoomName(int isShow) {
        List<String> resultList = new ArrayList<String>();
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }

        for (Room room : list) {
            if (room.getIsShow() == isShow) {
                resultList.add(room.getRoomName());
            }
        }
        return resultList;
    }

    public Room getArceById(int Id) {
        Room room = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }

        for (Room _room : list) {
            if (_room.getId() == Id) {
                room = _room;
                break;
            }

        }
        return room;
    }

    public String getRoomById(int Id) {
        String roomName = "客厅";
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getRoom();
        }

        for (Room _room : list) {
            if (_room.getId() == Id) {
                roomName = _room.getRoomName();
                break;
            }

        }
        return roomName;
    }

    public static void clearList() {
        list = WidgetDAOProxy.getInstance().getRoom();
    }

    public static List<Room> getSelect() {
        List<Room> resultList = new ArrayList<Room>();

        Room room = new Room();
        room.setRoomName(MyApplication.getInstance().getString(R.string.help_all));
        room.setPrimary_Key("FF");
        resultList.add(room);

        Room arce1 = new Room();
        arce1.setRoomName(MyApplication.getInstance().getString(R.string.switch_code));
        arce1.setPrimary_Key("00");
        resultList.add(arce1);

        Room arce2 = new Room();
        arce2.setRoomName(MyApplication.getInstance().getString(R.string.dimmer_code));
        arce2.setPrimary_Key("03");
        resultList.add(arce2);

        Room arce3 = new Room();
        arce3.setRoomName(MyApplication.getInstance().getString(R.string.ml_code));
        arce3.setPrimary_Key("04");
        resultList.add(arce3);

        Room arce4 = new Room();
        arce4.setRoomName(MyApplication.getInstance().getString(R.string.control_code));
        arce4.setPrimary_Key("19");
        resultList.add(arce4);

        Room arce5 = new Room();
        arce5.setRoomName(MyApplication.getInstance().getString(R.string.sence_code));
        arce5.setPrimary_Key("32");
        resultList.add(arce5);

        return resultList;
    }
}
